package awd.bj.jls.webs.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import awd.bj.jls.webs.api.ManagerApi;
import awd.bj.jls.webs.api.model.User;
import awd.bj.jls.webs.utils.ConstantUtils;
import awd.bj.jls.webs.utils.MenuApp;
import awd.bj.jls.webs.utils.PagerResult;
import awd.bj.jls.webs.utils.QueryParam;
import awd.bj.jls.webs.utils.ResponseMessage;
import awd.bj.jls.webs.utils.TermType;
import awd.bj.manager.model.MenusModel;
import awd.bj.manager.model.UsersettingModel;


@Controller
public class WelcomeController extends BaseAction{
	
	@Autowired
	Environment  evn;
	@Autowired
	private ManagerApi managerApi;
	
	@RequestMapping("/index.html")
	public String index(Model model,HttpServletRequest request) {
		User users = this.getCurrentUser();
		model.addAttribute("userName",users.getUsername());	
		model.addAttribute("reporturl", evn.getProperty("awd.report.url"));
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name)) {
			if("2".equals(name)) {
				return "test";
			}else {
				return "index";
			}
		}else {
			List<UsersettingModel> list=users.getSetting();
			if(list==null) {
				return "index";
			}else {
				for (UsersettingModel usersettingModel : list) {
					if(usersettingModel.getApp().equals(ConstantUtils.APPCODE)&&usersettingModel.getKey().equals("menue_type")&&usersettingModel.getValue().equals("1")) {
						return "index";
					}
					if(usersettingModel.getApp().equals(ConstantUtils.APPCODE)&&usersettingModel.getKey().equals("menue_type")&&usersettingModel.getValue().equals("2")) {
						return "index2";
					}
					System.err.println("所有设置");
				}
				return "index";
			}
		}
	}
	@RequestMapping("/")
	public String logout(Model model,HttpServletRequest request) {
		User users = getCurrentUser();
		model.addAttribute("userName",users.getUsername());	
		model.addAttribute("reporturl", evn.getProperty("awd.report.url"));
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name)) {
			if("2".equals(name)) {
				return "index2";
			}else {
				return "index";
			}
		}else {
			List<UsersettingModel> list=users.getSetting();
			if(list==null) {
				return "index";
			}else {
				for (UsersettingModel usersettingModel : list) {
					if(usersettingModel.getApp().equals(ConstantUtils.APPCODE)&&usersettingModel.getKey().equals(ConstantUtils.USER_SETTING_MENUTYPE_KEY)&&usersettingModel.getValue().equals("1")) {
						return "index";
					}
					if(usersettingModel.getApp().equals(ConstantUtils.APPCODE)&&usersettingModel.getKey().equals(ConstantUtils.USER_SETTING_MENUTYPE_KEY)&&usersettingModel.getValue().equals("2")) {
						return "index2";
					}
					System.err.println("所有设置");
				}
				return "index";
			}
		}
	}
	
	@RequestMapping("/js/common.js")
	public void common(Model model,HttpServletRequest request,HttpServletResponse respone) {
		User users = this.getCurrentUser();
		respone.setContentType("text/javascript");
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("appcode", ConstantUtils.APPCODE);
		map.put("user", JSON.toJSONString(users.getUserinfo()));
		map.put("group", JSON.toJSONString(users.getGroup()));
		map.put("menus", JSON.toJSONString(users.getMenus()));
		map.put("roles", JSON.toJSONString(users.getRoles()));
		map.put("settings", JSON.toJSONString(users.getSetting()));
        PrintWriter out;
		try {
			ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("/templates");
			Configuration cfg = Configuration.defaultConfiguration();
			GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
			Template t = gt.getTemplate("/common.html");
			t.binding(map);
			String str = t.render();
			out = respone.getWriter();
			out.println(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/getallmenu")
	@ResponseBody
	public Map<String, Object> getallmenu(Model model,HttpServletRequest request) {
		User users = this.getCurrentUser();
		Map<String, Object> result=new HashMap<String, Object>();
		List<Map<String, Object>> groups=new ArrayList<Map<String,Object>>();
		Map<String, Object> group1=new HashMap<String, Object>();
		Map<String, Object> group2=new HashMap<String, Object>();
		Map<String, Object> group3=new HashMap<String, Object>();
		group1.put("id", "1");group1.put("name", "出入所管理");group1.put("index", 1);
		group2.put("id", "2");group2.put("name", "相关业务");group2.put("index", 2);
		group3.put("id", "3");group3.put("name", "其它业务");group3.put("index", 3);
		groups.add(group1);
		groups.add(group2);
		groups.add(group3);

		List<MenuApp> apps=new ArrayList<MenuApp>();
		apps.addAll(getmenus(ConstantUtils.APPCODE));
		result.put("groups", groups);
		result.put("apps", apps);		
		return result;
	}
	
	private List<MenuApp> getmenus(String aPPCODE) {
		QueryParam param=new QueryParam();
		param.setPaging(false);
		param.and("appcode", TermType.eq, aPPCODE);
		ResponseMessage<PagerResult<MenusModel>> respone=managerApi.menusQuery(param);
		if(respone!=null&&respone.getStatus()==200) {
			List<MenuApp> result =new ArrayList<MenuApp>();
			List<MenusModel> list=respone.getResult().getData();
			for (MenusModel menusModel : list) {
				MenuApp menu=new MenuApp();
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_CRSGL_RSDJ)) {
					menu.setBgcolor("rgba(31, 83, 160,1)");
					menu.setColor("rgb(255, 255, 255)");
					menu.setGroup("1");
					menu.setIcon("resources2/images/imgs/rsdj.png");
					menu.setImage("resources2/images/apps/images/adf28bd452ff464a0152ff4ab9320000.png");
					menu.setIndex("1");
					menu.setSize("mini");					
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_CRSGL_CSGL)) {
					menu.setBgcolor("rgba(51, 122, 183,1)");
					menu.setColor("rgb(255, 255, 255)");
					menu.setGroup("1");
					menu.setIcon("resources2/images/imgs/csgl.png");
					menu.setImage("resources2/images/apps/images/jiaoy.png");
					menu.setIndex("2");
					menu.setSize("mini");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_CRSGL_LSCS)) {
					menu.setBgcolor("rgba(51, 122, 183,1)");
					menu.setColor("rgb(255, 255, 255)");
					menu.setGroup("1");
					menu.setIcon("resources2/images/imgs/lscs.png");
					menu.setImage("resources2/images/apps/images/adf28bd454b9af650154bf26774f0053.png");
					menu.setIndex("3");
					menu.setSize("mini");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_CRSGL_LSHJ)) {
					menu.setBgcolor("rgba(31,83,160,1)");
					menu.setColor("rgb(250,250,250)");
					menu.setGroup("1");
					menu.setIcon("resources2/images/imgs/lshj.png");
					menu.setImage("resources2/images/apps/images/adf28bd453021445015306f3fefa0003.png");
					menu.setIndex("4");
					menu.setSize("mini");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_CRSGL_LSWGDJ)) {
					menu.setBgcolor("rgba(31,83,160,1)");
					menu.setColor("rgb(250,250,250)");
					menu.setGroup("1");
					menu.setIcon("resources2/images/imgs/lswgdj.png");
					menu.setImage("resources2/images/apps/images/adf28bd454b9af650154bf29e9b30057.png");
					menu.setIndex("5");
					menu.setSize("mini");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_CRSGL_TS)) {
					menu.setBgcolor("rgb(31,83,160)");
					menu.setColor("rgb(255, 255, 255)");
					menu.setGroup("1");
					menu.setIcon("resources2/images/imgs/ts.png");
					menu.setImage("resources2/images/apps/images/adf28bd453021445015306eb38ce0000.png");
					menu.setIndex("6");
					menu.setSize("mini");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_CRSGL_JSHJ)) {
					menu.setBgcolor("rgba(31,83,160,1)");
					menu.setColor("rgb(255, 255, 255)");
					menu.setGroup("1");
					menu.setIcon("resources2/images/imgs/jshj.png");
					menu.setImage("resources2/images/apps/images/adf28bd454b9af650154bf20d086004f.png");
					menu.setIndex("7");
					menu.setSize("mini");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_XGYW_SJYY)) {
					menu.setBgcolor("rgb(3,0,166)");
					menu.setColor("rgb(255, 255, 255)");
					menu.setGroup("2");
					menu.setIcon("resources2/images/imgs/sjyy.png");
					menu.setImage("resources2/images/apps/images/adf28bd453021445015306eb38ce0000.png");
					menu.setIndex("1");
					menu.setSize("default");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_XGYW_TLHMC)) {
					menu.setBgcolor("rgba(65, 155, 199,1)");
					menu.setColor("rgb(255, 255,255)");
					menu.setGroup("2");
					menu.setIcon("resources2/images/imgs/tlhmc.png");
					menu.setImage("resources2/images/apps/images/adf28bd453021445015306fa7dc40006.png");
					menu.setIndex("2");
					menu.setSize("mini");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_XGYW_YQ)) {
					menu.setBgcolor("rgba(255,255,255,1)");
					menu.setColor("rgb(0, 0, 0)");
					menu.setGroup("2");
					menu.setIcon("resources2/images/imgs/yq.png");
					menu.setImage("resources2/images/apps/images/adf28bd454b9af650154bf29e9b30057.png");
					menu.setIndex("3");
					menu.setSize("mini");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_XGYW_PJDJ)) {
					menu.setBgcolor("rgba(31,83,160,1)");
					menu.setColor("rgb(250, 250,250)");
					menu.setGroup("2");
					menu.setIcon("resources2/images/imgs/pjdj.png");
					menu.setImage("resources2/images/apps/images/adf28bd454b9af650154bf29e9b30057.png");
					menu.setIndex("4");
					menu.setSize("mini");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_XGYW_TSZGL)) {
					menu.setBgcolor("rgba(34, 160, 31,1)");
					menu.setColor("rgb(255, 255, 255)");
					menu.setGroup("2");
					menu.setIcon("resources2/images/imgs/tszgl.png");
					menu.setImage("resources2/images/apps/images/adf28bd453021445015306f3fefa0003.png");
					menu.setIndex("5");
					menu.setSize("mini");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_XGYW_HJBD)) {
					menu.setBgcolor("rgba(0,117,169,1)");
					menu.setColor("rgb(250, 250,250)");
					menu.setGroup("2");
					menu.setIcon("resources2/images/imgs/hjbd.png");
					menu.setImage("resources2/images/apps/images/adf28bd454b9af650154bf29e9b30057.png");
					menu.setIndex("6");
					menu.setSize("mini");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_QTYW_CWCX)) {
					menu.setBgcolor("rgba(217,18, 54,1)");
					menu.setColor("rgb(255, 255, 255)");
					menu.setGroup("3");
					menu.setIcon("resources2/images/imgs/cwcx.png");
					menu.setImage("resources2/images/apps/images/adf28bd454b9af650154bf26774f0053.png");
					menu.setIndex("1");
					menu.setSize("mini");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_QTYW_JSBJD)) {
					menu.setBgcolor("rgba(134,66,36,1)");
					menu.setColor("rgb(250,250,250)");
					menu.setGroup("3");
					menu.setIcon("resources2/images/imgs/jsbjd.png");
					menu.setImage("resources2/images/apps/images/adf28bd454a540dc0154a54c84550000.png");
					menu.setIndex("2");
					menu.setSize("mini");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_QTYW_JWZXSQGL)) {
					menu.setBgcolor("rgb(31,83,160)");
					menu.setColor("rgb(255, 255, 255)");
					menu.setGroup("3");
					menu.setIcon("resources2/images/imgs/jqzx.png");
					menu.setImage("resources2/images/apps/images/adf28bd453021445015306eb38ce0000.png");
					menu.setIndex("3");
					menu.setSize("default");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_QTYW_LYB)) {
					menu.setBgcolor("rgb(39,73,194)");
					menu.setColor("rgb(255, 255, 255)");
					menu.setGroup("3");
					menu.setIcon("resources2/images/imgs/person-woman-identity-verified-approved@256px.png");
					menu.setImage("resources2/images/apps/images/adf28bd454b9af650154bf224eca0050.png");
					menu.setIndex("4");
					menu.setSize("default");
				}
				if(menusModel.getMenu().equals(ConstantUtils.MENUS_QTYW_SHFF)) {
					menu.setBgcolor("rgb(39,73,194)");
					menu.setColor("rgb(255, 255, 255)");
					menu.setGroup("3");
					menu.setIcon("resources2/images/imgs/person-woman-identity-verified-approved@256px.png");
					menu.setImage("resources2/images/apps/images/adf28bd454b9af650154bf224eca0050.png");
					menu.setIndex("4");
					menu.setSize("default");
				}
				menu.setId(menusModel.getMenu());
				menu.setBottom("0");
				menu.setClick("open");
				menu.setCode("");
				menu.setHeight("0");
				menu.setHidden(false);
				menu.setHtml("");
				menu.setLeft("0");
				menu.setLoadType("");
				menu.setMaximizable(true);
				menu.setMaximized(false);
				menu.setMode("window");
				menu.setName(menusModel.getMenuname());
				menu.setNative(false);
				menu.setPath(menusModel.getUrl());
				menu.setQuick(false);
				menu.setRenderer("");
				menu.setResizable(true);
				menu.setRight("0");
				menu.setScript("");				
				menu.setTile("");
				menu.setToggle("");
				menu.setTop("0");
				menu.setTpl("");
				menu.setUi("1");
				menu.setUrl("");
				menu.setWidth("0");
				result.add(menu);
			}			
			return result;			
		}
		return new ArrayList<MenuApp>();
	}
	
}
